package when3meet.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import client.ServerAccess;

import when3meet.model.ClientInfo;
import when3meet.view.Application;
import when3meet.view.MainFrame;
import xml.Message;

/**
 * Deal with GUI-based action when addChoiceButton is clicked. Handle add choice
 * response from server
 * 
 * @author Xi
 * 
 */
public class AddChoiceController implements ActionListener {

	public MainFrame mainFrame;
	public ServerAccess access;

	public AddChoiceController(MainFrame mainFrame) {

		this.mainFrame = mainFrame;
	}

	/**
	 * Handle add choice response from server,
	 * 
	 * @param response
	 */
	public void process(Message response) {

		Node update = response.contents.getFirstChild();
		ClientInfo ci = ClientInfo.getInstance();

		NamedNodeMap map = update.getAttributes();

		String id = map.getNamedItem("id").getNodeValue();
		// String num = map.getNamedItem("number").getNodeValue();
		String choice = map.getNamedItem("choice").getNodeValue();

		if (ci.getEventID() == id || ci.getEventID().equals(id)) {

			// expand current number of choices by 1, update choices in local
			// model
			ci.setCurrentChoiceNum();
			String[] temp;
			if (ci.getChoices() == null) {
				temp = new String[1];
				temp[0] = choice;
			} else {
				temp = new String[ci.getChoices().length + 1];
				for (int i = 0; i < temp.length - 1; i++)
					temp[i] = ci.getChoices()[i];

				temp[temp.length - 1] = choice;
			}

			ci.setChoices(temp);

			MainFrame.getInstance().getDecisionLineZone().setChoices(temp);
		}

		// if current number of choice equals to max allowed choice number,
		// disable add choice button
		if (ci.getCurrentChoiceNum() == ci.getNumChoices()) {
			MainFrame.getInstance().getChoiceField().setEnabled(false);
			MainFrame.getInstance().getAddChoiceButton().setEnabled(false);

			// if the mode is round-robin, moderator could add the edge first

			if (ci.getBehavior().equals("roundRobin") || ci.getBehavior() == "roundRobin")
				if (ci.isModerator() == true) {
					// MainFrame.getInstance().getAddEdgeButton().setEnabled(true);
					// MainFrame.getInstance().getDecisionLineZone().setCurrentAllowedEdge(1);
				} else
					MainFrame.getInstance().getAddEdgeButton().setEnabled(false);

			// if the mode is asynchronous, everyone could start adding edge.
			else {
				MainFrame.getInstance().getAddEdgeButton().setEnabled(true);
				// MainFrame.getInstance().getDecisionLineZone().setCurrentAllowedEdge(1);
			}

		}

	}

	@Override
	public void actionPerformed(ActionEvent arg0) {

		// for test
		// String[] ss = new String[3];
		// ss[0] = "diablo";
		// ss[1] = "nfs";
		// ss[2] = "pes";
		//
		// ci.setChoices(ss);

		// send XML request
		sendRequest();

	}

	/**
	 * send XML request
	 */
	public void sendRequest() {
		ClientInfo ci = ClientInfo.getInstance();

		String xmlString = Message.requestHeader() + "<addChoiceRequest " + "id = '" + ci.getEventID() + "' "
				+ "number = '" + ci.getCurrentChoiceNum() + "' " + "choice = '"
				+ MainFrame.getInstance().getChoiceField().getText() + "'/>" + "</request>";

		Message m = new Message(xmlString);

		access = Application.getInstance().getServerAccess();
		access.sendRequest(m);

	}

}
